package com.saguitech.sgemeos.ui.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.saguitech.sgemeos.R;
import com.saguitech.sgemeos.business.LoginBusiness;
import com.saguitech.sgemeos.utils.MenuBase;
import com.saguitech.sgemeos.utils.MenuBase.TargetActivity;

public class MapActivity extends Activity {

	private final String mapURL = "http://secompufs.com/images/stories/mapa.png";
	
	LoginBusiness login;
	Intent it;
	
	ProgressDialog progressBar;  

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		WebView map = (WebView) findViewById(R.id.mapView);
		map.getSettings().setBuiltInZoomControls(true);


		WebSettings settings = map.getSettings();
        settings.setJavaScriptEnabled(true);
        map.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
 
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
 
        progressBar = ProgressDialog.show(MapActivity.this, "", getText(R.string.loadMapMessage));
 
        map.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
 
            public void onPageFinished(WebView view, String url) {
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }
 
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                alertDialog.setMessage(getText(R.string.errorMapMessage));
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
                alertDialog.show();
            }
        });
        map.loadUrl(mapURL);

		login = (LoginBusiness) getIntent().getSerializableExtra("login");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(MenuBase.getLayoutResource(), menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuBase.adjustMenu(TargetActivity.MAP, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		MenuBase.onItemSelected(MapActivity.this, item);
		if (MenuBase.isIntentPrepared()) {
			MenuBase.getIntent().putExtra("login", login);
			MenuBase.commitIntent(MapActivity.this);
		}
		return true;
	}
	
}
